<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/crm/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/crm/css/public.css" media="all">
    <link rel="stylesheet" href="/static/crm/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main" style="padding-top: 20px;">
            <fieldset class="table-search-fieldset" style="display: none;">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">ID</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="id" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">权限名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="auth_name" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">权限备注</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="remark" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">用户状态</label>
                                <div class="layui-input-inline">
                                    <select name="status" lay-filter="province" id="province">
                                        <option value="">请选择</option>
                                        <option value="0">开启</option>
                                        <option value="1">关闭</option>
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">创建时间</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="create_time" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button lay-event="refresh" class="layui-btn layui-btn-sm layuimini-btn-primary" style="background-color:black;"><i class="fa fa-refresh layui-anim-rotate"></i> </button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
            
        </script>
            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
            <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs " lay-event="authorize">授权</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        </div>
    </div>
    <script src="/static/crm/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script type="text/html" id="status">
        <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" {{ d.status == "0" ? "checked" : "" }} lay-filter="ShowDemo">
    </script>
    <script>
        layui.use(['form', 'table', 'jquery', 'layer'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                layer = layui.layer;

            table.render({
                elem: '#currentTableId',
                url: '/crm/auth.role/index',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '搜索',
                    layEvent: 'search',
                    icon: 'layui-icon-search'
                }],
                cols: [[
                    { type: "checkbox", width: 50 },
                    { field: 'id', title: 'ID', sort: true, align: "center" },
                    { field: 'auth_name', title: '权限名称', align: "center" },
                    { field: 'remark', title: '备注信息', align: "center" },
                    { field: 'status', title: '状态', align: "center", templet: '#status' },
                    { field: 'create_time', title: '创建时间', align: "center" },
                    { title: '操作', minWidth: 200, toolbar: '#currentTableBar', align: "center" }
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                parseData: function (res) {
                    var result;
                    if (this.page.curr) {
                        result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                    } else {
                        result = res.data.slice(0, this.limit);
                    }
                    return { 'code': res.code, 'count': res.count, 'data': result };
                },
                skin: 'line',

            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                //执行搜索重载
                table.reload('currentTableId', {
                    page: { curr: 1 },
                    where: data.field
                }, 'data');
                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '角色添加',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['70%', '80%'],
                        content: '/crm/auth.role/add',
                        end: function () {
                            table.reload('currentTableId', {
                                page: { curr: 1 }
                            });
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'refresh') {
                    table.reload('currentTableId', {
                        page: { curr: 1 }
                    });
                } else if (obj.event === 'search') {
                    $(".table-search-fieldset").toggle();
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    var index = layer.open({
                        title: '编辑用户',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['70%', '80%'],
                        content: '/crm/auth.role/edit/id/' + data.id,
                        end: function () {
                            table.reload('currentTableId', {
                                page: { curr: 1 }
                            });
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('是否删除？', function (index) {
                        $.ajax({
                            url: '/crm/auth.role/delete/id/' + data.id,
                            dataType: 'json',
                            success: function (result) {
                                if (result.code == 1) {
                                    layer.msg('删除成功')
                                    obj.del()
                                    layer.close(index)
                                    table.reload('currentTableId', {
                                        page: { curr: 1 }
                                    });
                                } else {
                                    layer.msg('删除失败')
                                }
                            }
                        })
                    })
                } else if (obj.event === 'authorize') {
                    var index = layer.open({
                        title: '角色授权',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['70%', '80%'],
                        content: '/crm/auth.role/authorize/id/' + data.id,
                        end: function () {
                            window.location.reload();
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                }
            });

        });
    </script>

</body>

</html>